package com.store.pet.petstore.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.gyf.barlibrary.ImmersionBar;
import com.store.pet.petstore.R;
import com.store.pet.petstore.fragment.BaseFragment;
import com.store.pet.petstore.fragment.CartFragment;
import com.store.pet.petstore.fragment.HomeFragment;
import com.store.pet.petstore.fragment.MessageFragment;
import com.store.pet.petstore.fragment.PersonFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.store.pet.petstore.activity.PetListActivity.BUY_SUCCESS;
import static com.store.pet.petstore.fragment.PersonFragment.ADD_COMMODITY;
import static com.store.pet.petstore.fragment.PersonFragment.CHANGE_SUCCESS;

/**
 * 首页Activity
 */
public class HomeActivity extends BaseActivity {


    @BindView(R.id.my_status)
    View myStatus;
    @BindView(R.id.framelayout)
    FrameLayout framelayout;
    @BindView(R.id.bottombar_main)
    BottomNavigationBar bottombarMain;

    private HomeFragment homeFragment;  //首页

    private MessageFragment messageFragment;  //消息

    private CartFragment cartFragment;  //购物车

    private PersonFragment personFragment;  //个人

    private List<BaseFragment> fragmentList = new ArrayList<>();  //存放Fragment的集合

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_home;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //沉浸式标题栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
        takeRelativeStatusBar(myStatus);
        initBottomBar();
    }

    @Override
    public void loadData() {

    }

    //底部导航栏的应用 #EF7E10点击 #818181未点击
    private void initBottomBar(){
        bottombarMain.setMode(BottomNavigationBar.MODE_FIXED);
        bottombarMain.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottombarMain.setBackgroundColor(getResources().getColor(R.color.White));
        bottombarMain.addItem(new BottomNavigationItem(R.drawable.shouye_pressed, "首页").setInactiveIconResource(R.drawable.shouye_normal).setActiveColor("#EF7E10"))
                .addItem(new BottomNavigationItem(R.drawable.xiaoxi_pressed, "消息").setInactiveIconResource(R.drawable.xiaoxi_normal)).setActiveColor("#EF7E10")
                .addItem(new BottomNavigationItem(R.drawable.gouwuche_pressed, "购物车").setInactiveIconResource(R.drawable.gouwuche_normal)).setActiveColor("#EF7E10")
                .addItem(new BottomNavigationItem(R.drawable.wode_pressed, "我的").setInactiveIconResource(R.drawable.wode_normal)).setActiveColor("#EF7E10")
                .setFirstSelectedPosition(0)
                .initialise();
        onTabSelected(0);
        bottombarMain.selectTab(0);
        bottombarMain.setTabSelectedListener(this);
    }

    public void hideFragment(FragmentTransaction transaction) {
        for (Fragment fragment : fragmentList) {
            transaction.hide(fragment);
        }
    }

    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.framelayout, homeFragment);
                    fragmentList.add(homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                if (messageFragment == null) {
                    messageFragment = new MessageFragment();
                    transaction.add(R.id.framelayout, messageFragment);
                    fragmentList.add(messageFragment);
                } else {
                    transaction.show(messageFragment);
                }
                break;
            case 2:
                if (cartFragment == null) {
                    cartFragment = new CartFragment();
                    transaction.add(R.id.framelayout, cartFragment);
                    fragmentList.add(cartFragment);
                } else {
                    transaction.show(cartFragment);
                }
                break;
            case 3:
                if (personFragment == null) {
                    personFragment = new PersonFragment();
                    transaction.add(R.id.framelayout, personFragment);
                    fragmentList.add(personFragment);
                } else {
                    transaction.show(personFragment);
                }
                break;
        }
        transaction.commit();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(data != null){
            //刷新数据
            switch (resultCode){
                case BUY_SUCCESS:
                    homeFragment.loadData();
                    break;
                case ADD_COMMODITY:
                    homeFragment.loadData();
                    break;
                case CHANGE_SUCCESS:
                    personFragment.gotoLoginActivity();
                    break;
            }
        }
    }
}
